package com.shunhao.surface

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import com.shunhao.base.BaseActivity
import com.shunhao.model.EventCenter
import com.shunhao.rxlibrary.R
import com.shunhao.surface.NotFoundFragment.Companion.create

/**
 * 描述： 单一Fragment的Activity
 *
 * @author Cuizhen
 * @date 2019/4/9
 */
class SingleFragmentActivity : BaseActivity() {

    private var mFragment: Fragment? = null

    override fun isBindEventBusHere(): Boolean = false

    override fun getContentViewLayoutId(): Int = R.layout.activity_single_frag

    override fun initViews() {
        val fragmentClassName = intent.getStringExtra("fragmentClassName")
        try {
            val clazz = Class.forName(fragmentClassName) as Class<Fragment>
            mFragment = clazz.newInstance()
        } catch (ignore: Exception) {
        }
        if (mFragment == null) {
            mFragment = create(fragmentClassName)
        }
        val fm = supportFragmentManager
        val ft = fm.beginTransaction()
        ft.replace(R.id.basic_core_fl_fragment, mFragment!!)
        ft.commit()
    }

    override fun initData() {}

    override fun onEventComing(eventCenter: EventCenter<Any>) {
    }


    companion object {
        fun start(context: Context, fragmentClass: Class<Fragment?>) {
            val intent = Intent(context, SingleFragmentActivity::class.java)
            intent.putExtra("fragmentClassName", fragmentClass.name)
            context.startActivity(intent)
        }
    }
}